package com.l.web.master.system.role;

import com.l.web.common.ResponseCode;
import com.l.web.common.ResponseInfo;
import org.bson.types.ObjectId;
import org.l.qiao.Cluster;
import org.l.qiao.builder.Field;
import org.l.qiao.builder.Query;
import org.l.qiao.builder.Sort;
import org.l.qiao.builder.Update;
import org.l.qiao.file.Limit;
import org.l.qiao.util.ClusterUtil;
import org.l.qiao.web.KeyUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RoleService {

    @Autowired
    private RoleDAO roleDAO;

    public List<Cluster> getList() {
        return getList(null, null, null);
    }

    public List<Cluster> getList(Query query, Field field, Sort sort) {
        List<Cluster> roles = roleDAO.find(query, field, sort);
        return roles;
    }

    public boolean detail(String id, Map modelMap) {
        Cluster role = roleDAO.findById(id);
        if (role.isEmpty()) {
            return false;
        }

        List<Cluster> limitModules = initLimit(role);
        modelMap.put("limitConfig", limitModules);
        modelMap.put("role", role);
        modelMap.put("check", ClusterUtil.getCheck(Role.class));
        return true;
    }

    public List<Cluster> initLimit(Cluster role) {
//        Map<String, Object> limit = role.getMap(Role.LIMIT, new HashMap());
//        Limit limitLoad = new Limit();
//
//        if (limitLoad.load()) {
//            List<Cluster> limits = limitLoad.getLimit();
//            List<Cluster> limitModules = new ArrayList<>();
//
//            for (Cluster module : limits) {
//                boolean contain = false;
//
//                for (Object object : module.getList("action", new ArrayList())) {
//                    Map<String, Object> action = (Map<String, Object>) object;
//                    if (limit.containsKey(action.get("key"))) {
//                        contain = true;
//                        break;
//                    }
//                }
//
//                if (contain) {
//                    limitModules.add(module);
//                }
//            }
//
//            return limitModules;
//        }

        return null;
    }


    public ResponseInfo save(Role role) {
        ResponseInfo rj = new ResponseInfo();
        String parentid = role.getString(Role.PARENT_ID, "");
        List parent;

        if (parentid.equals("")) {
            parent = new ArrayList();
            role.remove(Role.PARENT_ID);
        } else {
            Cluster cluster = roleDAO.findById(parentid);

            if (cluster.isEmpty()) {
                rj.setCode(ResponseCode.EXIST_NOT);
                return rj;
            }

            parent = cluster.getList(Role.PARENT, new ArrayList());
            parent.add(cluster.get(Role.ID));
            role.put(Role.PARENT_ID, cluster.get(Role.ID));
        }

        String id = role.setId();
        parent.add(role.get(Role.ID));
        role.put(Role.PARENT, parent);
        role.put(Role.DEEP, parent.size());
        role.put(Role.LIMIT, new HashMap());
        role.put(Role.CREATED, new Date());
        roleDAO.insert(role);
        rj.setCode(ResponseCode.FINISH);
        rj.put("id", id);
        return rj;
    }

    public int update(KeyUpdate keyUpdate) throws Exception {
        int result = ResponseCode.FINISH;

        if ("LIMIT".equals(keyUpdate.getName())) {
            result = updateLimit(keyUpdate);
        } else {
            roleDAO.update(keyUpdate);
        }

        return result;
    }

    private int updateLimit(KeyUpdate keyUpdate) throws Exception {
        Query query = new Query();
        query.idEQ(keyUpdate.getId());
        Update update = new Update(keyUpdate.getName(), parseMap(keyUpdate));
        roleDAO.updateSetOne(query, update);
        return ResponseCode.FINISH;
    }

    private Map parseMap(KeyUpdate keyUpdate) {
        Map limitMap = new HashMap();
//        JSONObject jsonValue = new JSONObject(keyUpdate.getValue());
//        Iterator<String> iterator = jsonValue.keys();
//
//        while (iterator.hasNext()) {
//            String key = iterator.next();
//            JSONArray array = jsonValue.getJSONArray(key);
//            List<String> page = new ArrayList<>();
//
//            for (int i = 0; i < array.length(); i++) {
//                page.add(array.getString(i));
//            }
//
//            limitMap.put(key, page);
//        }

        return limitMap;
    }

    public List<Cluster> limit() {
        Limit limit = new Limit();
        List<Cluster> limitConfig = new ArrayList();
//
//        if (limit.load()) {
//            limitConfig = limit.getLimit();
//        }

        return limitConfig;
    }

    public List parent(String id) {
        ObjectId objectId = new ObjectId(id);
        Query query = new Query();
        query.ne(Role.PARENT, objectId);
        query.ne(Role.ID, objectId);
        Field field = new Field();
        field.include(Role.ID, Role.NAME, Role.PARENT_ID);
        List roles = roleDAO.find(query, field, null);
        return roles;
    }

    public int remove(String id) {
        Query query = new Query();
        query.eq(Role.PARENT_ID, id);
        int code;

        if (roleDAO.findOne(query).isEmpty()) {
            query.clear();
            query.idEQ(id);
            query.ne(Role.PARENT_ID, "");
            roleDAO.remove(query);
            code = ResponseCode.FINISH;
        } else {
            code = ResponseCode.REFUSE;
        }

        return code;
    }
}
